
package cn.edu.scau.cmi.hombio.dao;

import cn.edu.scau.cmi.hombio.domain.Function;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Function entities.
 * 
 */
@Repository("FunctionDAO")

@Transactional
public class FunctionDAOImpl extends AbstractJpaDao<Function> implements FunctionDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] {
			Function.class }));

	/**
	 * EntityManager injected by Spring for persistence unit pig
	 *
	 */
	@PersistenceContext(unitName = "pig")
	private EntityManager entityManager;

	/**
	 * Instantiates a new FunctionDAOImpl
	 *
	 */
	public FunctionDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findAllFunctions
	 *
	 */
	@Transactional
	public Set<Function> findAllFunctions() throws DataAccessException {

		return findAllFunctions(-1, -1);
	}

	/**
	 * JPQL Query - findAllFunctions
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Function> findAllFunctions(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllFunctions", startResult, maxRows);
		return new LinkedHashSet<Function>(query.getResultList());
	}

	/**
	 * JPQL Query - findFunctionByNameContaining
	 *
	 */
	@Transactional
	public Set<Function> findFunctionByNameContaining(String name) throws DataAccessException {

		return findFunctionByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findFunctionByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Function> findFunctionByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFunctionByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Function>(query.getResultList());
	}

	/**
	 * JPQL Query - findFunctionByMeno
	 *
	 */
	@Transactional
	public Set<Function> findFunctionByMeno(String meno) throws DataAccessException {

		return findFunctionByMeno(meno, -1, -1);
	}

	/**
	 * JPQL Query - findFunctionByMeno
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Function> findFunctionByMeno(String meno, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFunctionByMeno", startResult, maxRows, meno);
		return new LinkedHashSet<Function>(query.getResultList());
	}

	/**
	 * JPQL Query - findFunctionByName
	 *
	 */
	@Transactional
	public Set<Function> findFunctionByName(String name) throws DataAccessException {

		return findFunctionByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findFunctionByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Function> findFunctionByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFunctionByName", startResult, maxRows, name);
		return new LinkedHashSet<Function>(query.getResultList());
	}

	/**
	 * JPQL Query - findFunctionByPrimaryKey
	 *
	 */
	@Transactional
	public Function findFunctionByPrimaryKey(Integer id) throws DataAccessException {

		return findFunctionByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findFunctionByPrimaryKey
	 *
	 */

	@Transactional
	public Function findFunctionByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFunctionByPrimaryKey", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.Function) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findFunctionByMenoContaining
	 *
	 */
	@Transactional
	public Set<Function> findFunctionByMenoContaining(String meno) throws DataAccessException {

		return findFunctionByMenoContaining(meno, -1, -1);
	}

	/**
	 * JPQL Query - findFunctionByMenoContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Function> findFunctionByMenoContaining(String meno, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findFunctionByMenoContaining", startResult, maxRows, meno);
		return new LinkedHashSet<Function>(query.getResultList());
	}

	/**
	 * JPQL Query - findFunctionById
	 *
	 */
	@Transactional
	public Function findFunctionById(Integer id) throws DataAccessException {

		return findFunctionById(id, -1, -1);
	}

	/**
	 * JPQL Query - findFunctionById
	 *
	 */

	@Transactional
	public Function findFunctionById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findFunctionById", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.Function) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Function entity) {
		return true;
	}
}
